home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / trash-1.000 / trash-1 / trash-1.0 / trash < prev   
Encoding:
Text File  |  1995-01-25  |  3.9 KB  |  116 lines

  1. #!/bin/sh
  2. #
  3. ## trash -- an attempt at a trashcan command
  4. ## created 05-Aug-1994 jmk
  5. ## autodate: 25-Jan-1995
  6. ## autotime: 02:47
  7.  
  8. ## ----------------------------------------------------------------
  9. ## COPYRIGHT
  10. ## `trash' is written and maintained by Jim Knoble
  11. ## <jmknoble@mercury.interpath.net>.  `trash' is copyright 1995 by
  12. ## Jim Knoble; you may freely distribute and modify it, but the
  13. ## original code must bear my name.  Any modifications that you
  14. ## introduce must bear your name and must not bear mine.
  15. ## ----------------------------------------------------------------
  16.  
  17. ## the idea here is to move copies of things we want to delete
  18. ## into a hidden `trash' directory, keeping versions of things
  19. ## so that we can undelete them later if we need to.
  20.  
  21. ## this makes pathname patterns which match no files
  22. ## expand to '' rather than the pattern.
  23. ## we use this in the `--empty' command.
  24. allow_null_glob_expansion=1
  25.  
  26. ## we use a variable to indicate verbose use or not;
  27. ## if it's not set, default to verbose.
  28. if [ -z ${TRASH_VERBOSE} ]; then
  29.     export TRASH_VERBOSE="yes"
  30. fi
  31. case ${TRASH_VERBOSE} in
  32.     on | yes | true )
  33.         trash_verbose_cmd="-v"
  34.     ;;
  35.     off | no | false )
  36.         trash_verbose_cmd=""
  37.     ;;
  38.     * )
  39.         echo "${0}: error: environment variable TRASH_VERBOSE has bad value."
  40.     echo "${0}: value of \`$TRASH_VERBOSE' should be either \`on' or \`off'."
  41.     echo "${0}: defaulting to \`on'"
  42.     trash_verbose_cmd="-v"
  43.     ;;
  44. esac
  45.  
  46. ## if we don't have a trash directory defined, make a default
  47. if [ -z ${TRASH_DIR} ]; then
  48.     export TRASH_DIR="$HOME/.trash"
  49. fi
  50.  
  51. ## check that our trashcan is a valid directory...
  52. if [ ! -d $TRASH_DIR ]; then
  53.     echo "${0}: error: trashcan \`$TRASH_DIR' is not a directory."
  54.     echo "${0}: i can only use a trashcan that is a directory."
  55.     echo "${0}: you can set your trashcan with the TRASH_DIR environment variable."
  56.     exit 1
  57. elif [ "$TRASH_DIR" = "." -o "$TRASH_DIR" = "./" -o "$TRASH_DIR" = "/" ]; then
  58.     echo "${0}: error: trashcan \`$TRASH_DIR' is not a valid trash directory."
  59.     echo "${0}: try setting TRASH_DIR to a directory like \`~/.trash'."
  60.     exit 1
  61. else
  62.     ## check for trash commands or files to trash
  63.     trashcmd="$1"
  64.     if [ -z $trashcmd ]; then
  65.         trashcmd="--help"
  66.     fi
  67.     case $trashcmd in
  68.         --help )
  69.         ## display help
  70.             echo ""
  71.             echo "  usage: ${0} file [file...]"
  72.             echo "      moves <file> to \`trashcan' directory"
  73.             echo "      specified by environment variable \`TRASH_DIR'"
  74.             echo "      (defaults to ~/.trash)."
  75.             echo ""
  76.             echo "  usage: ${0} { --list | --view }"
  77.             echo "      show contents of \`trashcan' directory"
  78.             echo ""
  79.             echo "  usage: ${0} --empty"
  80.             echo "      remove all files from \`trashcan' directory"
  81.             echo ""
  82.         ;;
  83.     -l | --list | --view )
  84.         ## list trashcan contents
  85.         echo "contents of trashcan \`$TRASH_DIR':"
  86.         cd $TRASH_DIR
  87.         ## we want to make sure to catch hidden files,
  88.         ## so we use `-a'.
  89.         ls -la
  90.         ;;
  91.     --empty )
  92.             ## remove contents of trashcan
  93.             echo "emptying trashcan \`$TRASH_DIR' ..."
  94.         ## we want to catch both hidden and normal files...
  95.             for trash_files in $TRASH_DIR/.[^.]* $TRASH_DIR/*; do
  96.             ## we'll let ourselves know if there were any files
  97.         ## in the trashcan
  98.             trashed_one=1
  99.         ## we want to make sure to catch any subdirectories,
  100.         ## so we use `-r' to recurse.
  101.                 rm -r $trash_verbose_cmd $trash_files
  102.         done
  103.         if [ -z $trashed_one ]; then
  104.                 echo "${0}: trashcan was already empty."
  105.         fi
  106.             ;;
  107.         * )
  108.             ## we have files to trash, so move the arguments to the
  109.         ## trashcan.
  110.         ## `-f' forces moving (we don't care if we overwrite)
  111.         ## `-b' backs files up using `-V' version control
  112.             mv -f -b -V numbered $trash_verbose_cmd $* $TRASH_DIR
  113.         ;;
  114.     esac
  115. fi
  116.